package com.qyhl.module_home.utils.view;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.ImageView;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qyhl.module_home.R;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.config.MessageDetailBean;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.rd.PageIndicatorView;

import java.util.List;

import razerdp.basepopup.BasePopupWindow;

public class HomeMessagePop extends BasePopupWindow {
    private View popupView;
    private List<MessageDetailBean> mDataList;


    public HomeMessagePop(Activity context, List<MessageDetailBean> mDataList) {
        super(context);
        this.mDataList = mDataList;
        bindEvent(context);
    }

    @Override
    protected Animation initShowAnimation() {
        return getDefaultScaleAnimation();
    }

    @Override
    public View getClickToDismissView() {
        return null;
    }

    @Override
    public View onCreatePopupView() {
        popupView = LayoutInflater.from(getContext()).inflate(R.layout.home_pop_home_message, null);
        return popupView;
    }

    @Override
    public View initAnimaView() {
        return findViewById(R.id.popup_anima);
    }

    private void bindEvent(final Activity context) {
        ViewPager viewPager = popupView.findViewById(R.id.view_pager);
        ImageView btnClose = popupView.findViewById(R.id.close_btn);

        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return mDataList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, final int position) {
                RoundedImageView imageView = new RoundedImageView(context);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                imageView.setOval(false);
                imageView.setCornerRadius(6.0f);
                Glide.with(context)
                        .load(mDataList.get(position).getImage())
                        .into(imageView);

                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        skip(mDataList.get(position), context);
                    }
                });
                container.addView(imageView);
                return imageView;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((View) object);
            }
        });
        final PageIndicatorView indicator = popupView.findViewById(R.id.indicator);
        if (mDataList.size() > 1) {
            indicator.setVisibility(View.VISIBLE);
            indicator.setCount(mDataList.size());
        } else {
            indicator.setVisibility(View.GONE);
        }

        indicator.setViewPager(viewPager);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                indicator.setSelection(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }


    private void skip(MessageDetailBean bean, Activity context) {
        switch (bean.getContentType()) {
            case "appTop":
                break;
            case "broke":{
                Bundle bundle = new Bundle();
                bundle.putString("id", bean.getContentId()+"");
                RouterManager.router(ARouterPathConstant.SCOOP_DETAIL_ACTIVIT,bundle);

            }
                break;
            case "videoLive":{
                Bundle bundle = new Bundle();
                bundle.putString("id", bean.getContentId() + "");
                RouterManager.router(ARouterPathConstant.TELE_VIDEO_ACTIVITY,bundle);
            }
                break;
            case "imageLive":{
                Bundle bundle = new Bundle();
                bundle.putString("id", bean.getContentId() + "");
                RouterManager.router(ARouterPathConstant.TELE_VIDEO_ACTIVITY,bundle);
            }
                break;
            case "circle":{
                Bundle bundle = new Bundle();
                bundle.putString("id", bean.getContentId() + "");
                RouterManager.router(ARouterPathConstant.CIRCLE_DETAIL,bundle);
            }
                break;
            case "integral":
                break;
            case "cmsCommonNews":{
                Bundle bundle = new Bundle();
                bundle.putString("id", bean.getContentId() + "");
                RouterManager.router(ARouterPathConstant.NORMAL_NEWS_ACTIVITY,bundle);
            }
                break;
            case "cmsVideoNews":{
                Bundle bundle = new Bundle();
                bundle.putString("id", bean.getContentId() + "");
                RouterManager.router(ARouterPathConstant.VIDEO_NEWS_ACTIVITY,bundle);
            }
                break;
            case "recNews":{
                Bundle bundle = new Bundle();
                bundle.putString("id", bean.getContentId() + "");
                RouterManager.router(ARouterPathConstant.NEWS_CATCH,bundle);
            }
                break;
            case "link":{
                Bundle bundle = new Bundle();
                bundle.putString("title", bean.getTitle());
                bundle.putString("url", bean.getLink());
                bundle.putString("id", bean.getContentId() + "");
                bundle.putBoolean("hasShare", false);
                RouterManager.router(ARouterPathConstant.ADV,bundle);
            }
                break;
            case "activity":{
                Bundle bundle = new Bundle();
                bundle.putString("actId", bean.getContentId() + "");
                RouterManager.router(ARouterPathConstant.ACT_DETAILS,bundle);
            }
                break;
            case "lottery":{
                Bundle bundle = new Bundle();
                bundle.putString("title", bean.getTitle());
                bundle.putString("id", bean.getContentId() + "");
                RouterManager.router(ARouterPathConstant.LUCK_DRAW,bundle);
            }
                break;
            default:
                break;
        }
    }
}
